package com.jywl.util;

import java.util.List;

import com.jywl.entity.Column;

public class EntityUtil {
	
	public static String getProperty(List<Column> columns) {
		StringBuffer sb = new StringBuffer();
		for (Column column : columns) {
			sb.append(GeneratorUtil.getSpace(1));
			sb.append("private " +column.getJavatype()+" "+ column.getPropertyname()+";");
			sb.append("\r\n");
			
		}
		return sb.toString();
	}

	/**
	 * 
	public Integer getGender() {
		return gender;
	}
	public void setGender(Integer gender) {
		this.gender = gender;
	}
	@JsonSerialize(using=JsonDateSerializer.class)  
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	 * @param columns
	 * @return
	 */
	public static String getSetGet(List<Column> columns) {
		StringBuffer sb = new StringBuffer();
		for (Column column : columns) {
			String type = column.getJavatype();
			String strL = column.getPropertyname();	
			String strU = GeneratorUtil.captureName(strL);
			//set
			sb.append(GeneratorUtil.getSpace(1));
			sb.append("public void"+" set" + strU+"("+type+" "+strL+") {");
			sb.append("\r\n");
			sb.append(GeneratorUtil.getSpace(2));
			sb.append("this."+strL+" = "+strL+";");
			sb.append("\r\n");
			sb.append(GeneratorUtil.getSpace(1));
			sb.append("}\r\n");
			
			//get
			if("Date".equals(type)){
				sb.append(GeneratorUtil.getSpace(1)+"@JsonSerialize(using=JsonDateSerializer.class)"+"\r\n");
			}
			sb.append(GeneratorUtil.getSpace(1));
			sb.append("public "+ type +" get"+strU+"(){");
			sb.append("\r\n");
			sb.append(GeneratorUtil.getSpace(2));
			sb.append("return " + strL+";");
			sb.append("\r\n");
			sb.append(GeneratorUtil.getSpace(1));
			sb.append("}\r\n");
			
		}
		return sb.toString();
	}

	/**
	 * 
	 * 
	 * 
	@Override
	public String toString() {
		return "User [userid=" + userid + ", age=" + age + ", loginname=" + loginname + ", username=" + username
				+ ", gender=" + gender + ", birthday=" + birthday + ", status=" + status + ", remarks=" + remarks + "]";
	}
	
	 * @param columns
	 * @return
	 */
	public static String getToString(List<Column> columns,String classname) {
		StringBuffer sb = new StringBuffer();
		sb.append(GeneratorUtil.getSpace(1)+"@Override"+"\r\n");
		sb.append(GeneratorUtil.getSpace(1)+"public String toString() {"+"\r\n");
		sb.append(GeneratorUtil.getSpace(2)+"return \""+classname+" [");
		for (Column column : columns) {
			String strL = column.getPropertyname();			
			sb.append(strL+"=\" + "+strL+" +\", ");
		}
		sb.append("]\";\r\n");
		sb.append(GeneratorUtil.getSpace(1)+"}\r\n");
		return sb.toString();
	}

	public static String getQueryPro(List<Column> columns) {
		StringBuffer sb = new StringBuffer();
		String sp=",";
		int i=0;
		for (Column c : columns) {
			i++;
			if(i==columns.size()){
				sp="";
			}
			sb.append(GeneratorUtil.getSpace(3)+ c.getName() + " " + c.getPropertyname()+sp +"\r\n");
		}
		
		return sb.toString();
	}

	public static String getQueryWhere(List<Column> columns) {
	/*	<if test=" username != null and username != ''">
			and username like '%'||#{username}||'%'
		</if>*/
		StringBuffer sb = new StringBuffer();
		for (Column c : columns) {
			if("Y".equals(c.getIssearch())){
				sb.append(GeneratorUtil.getSpace(2)+"<if test=\" "+c.getPropertyname()+" != null and "+c.getPropertyname()+" != ''\">\r\n");
				sb.append(GeneratorUtil.getSpace(3)+"and "+ c.getName()+" like '%'||#{"+c.getPropertyname()+"}||'%'\r\n");
				sb.append(GeneratorUtil.getSpace(2)+"</if>\r\n");
			}
		}
		
		return sb.toString();
	}
	public static String getQueryWhereSearch(List<Column> columns) {
			StringBuffer sb = new StringBuffer();
			for (Column c : columns) {
				if("Y".equals(c.getIssearch())){
					sb.append(GeneratorUtil.getSpace(2)+"<if test=\" "+c.getPropertyname()+" != null and "+c.getPropertyname()+" != ''\">\r\n");
					sb.append(GeneratorUtil.getSpace(3)+"and "+ c.getSearchname()+" like '%'||#{"+c.getPropertyname()+"}||'%'\r\n");
					sb.append(GeneratorUtil.getSpace(2)+"</if>\r\n");
				}
			}
			return sb.toString();
		}


	public static String getQueryOrderBy(List<Column> columns) {
		StringBuffer sb = new StringBuffer();
		Column[] cs = new Column[3] ;
		boolean flag = false;
		for (Column c : columns) {
			if("1asc".equals(c.getOrderby())||"1desc".equals(c.getOrderby())){
				c.setOrderby(c.getOrderby().substring(1));
				cs[0] = c;
				flag = true;
			}
			if("2asc".equals(c.getOrderby())||"2desc".equals(c.getOrderby())){
				c.setOrderby(c.getOrderby().substring(1));
				cs[1] = c;
				flag = true;
			}
			if("3asc".equals(c.getOrderby())||"3desc".equals(c.getOrderby())){
				c.setOrderby(c.getOrderby().substring(1));
				cs[2] = c;
				flag = true;
			}
		}
		if(flag){
			sb.append(GeneratorUtil.getSpace(2)+"order by ");
		}
		
		for (Column c : cs) {
			if(null != c){
				sb.append(","+c.getPropertyname()+" "+ c.getOrderby()+" ");
			}
		}
		
		return sb.toString().replaceFirst(",", "");
	}
	
	
	public static String getAddCol(List<Column> columns) {
		StringBuffer sb = new StringBuffer();
		String sp=",";
		int i=0;
		for (Column c : columns) {
			i++;
			if(i==columns.size()){
				sp="";
			}
			sb.append(GeneratorUtil.getSpace(3)+ c.getName() +sp +"\r\n");
		}
		return sb.toString();
	}

	public static String getAddPro(List<Column> columns) {
		// #{item.userid,jdbcType=VARCHAR} USERID,
		StringBuffer sb = new StringBuffer();
		String sp=",";
		int i=0;
		for (Column c : columns) {
			i++;
			if(i==columns.size()){
				sp="";
			}
			sb.append(GeneratorUtil.getSpace(4)+ "#{item."+c.getPropertyname()+",jdbcType="+getJdbctype(c)+"} " +c.getName() +sp +"\r\n");
		}
		return sb.toString();
	}

	

	public static String getUpdateSet(List<Column> columns) {
		StringBuffer sb = new StringBuffer();
		for (Column c : columns) {
				if(null!=c.getIspk()&&1==c.getIspk()){
					continue;
				}
				// age = #{item.age},
				sb.append(GeneratorUtil.getSpace(2)+"<if test=\"item."+c.getPropertyname()+" != null and item."+c.getPropertyname()+" != ''\">\r\n");
				sb.append(GeneratorUtil.getSpace(3)+ c.getName()+" = #{item."+c.getPropertyname()+"},\r\n");
				sb.append(GeneratorUtil.getSpace(2)+"</if>\r\n");
		
		}
		
		return sb.toString();
	}

	public static String getUpdateSetPk(List<Column> columns) {
		String s = "";
		for (Column column : columns) {
			if(null!=column.getIspk()&& 1 ==column.getIspk()){
				s=(GeneratorUtil.getSpace(2) + column.getName()+" = #{item."+column.getPropertyname()+"}\r\n" );
			}
		}
		return s;
	}

	public static String getUpdateWhere(List<Column> columns) {
		// where t.userid = #{item.userid,jdbcType=VARCHAR}
		String s = "";
		for (Column column : columns) {
			if(null!=column.getIspk()&& 1 ==column.getIspk()){
				s=(" t." +column.getName()+" =  #{item."+column.getPropertyname()+",jdbcType="+getJdbctype(column)+"}\r\n");
			}
		}
		return s;
	}

	public static String getDeletePkCol(List<Column> columns) {
		String s = "";
		for (Column column : columns) {
			if(null!=column.getIspk()&& 1 ==column.getIspk()){
				s=column.getName();
			}
		}
		return s;
	}

	public static String getDeletePkPro(List<Column> columns) {
		//#{item.userid}
		String s = "";
		for (Column column : columns) {
			if(null!=column.getIspk()&& 1 ==column.getIspk()){
				s=column.getPropertyname();
			}
		}
		return s;
	}

	public static String getSearchTr(List<Column> columns, int searchCount) {
		
		if(0==searchCount){
			searchCount = 2;
		}
		StringBuffer sb = new StringBuffer();
		int count =0;
		for (Column c : columns) {
				if("Y".equals(c.getIssearch())){
					if(count%searchCount==0){
						sb.append(GeneratorUtil.getSpace(4)+"<tr>\r\n");
					}
					sb.append(GeneratorUtil.getSpace(5)+"<td>\r\n");
					
					//getEasyuiTyoe
					sb.append(GeneratorUtil.getSpace(6)+"<input class='"+ getEasyuiTyoe(c)
						+"'  label='"+(null==c.getComments()?c.getPropertyname():c.getComments())
								+":' id='"+c.getPropertyname()+"' name='"+c.getPropertyname()
								+"' data-options='labelWidth:60' style='width:180px'"+ ">\r\n");
					sb.append(GeneratorUtil.getSpace(5)+"</td>\r\n");
					count ++;
					if(count%searchCount==0){
						sb.append(GeneratorUtil.getSpace(4)+"</tr>\r\n");
					}
				}		
		}
		
		return sb.toString();
	}

	public static String getTableth(List<Column> columns) {
		StringBuffer sb = new StringBuffer();
		sb.append(GeneratorUtil.getSpace(4)+"<th data-options=\"field:'"+getDeletePkPro(columns)+"',checkbox:true\">ID</th>"+"\r\n");
		for (Column c : columns) {
				if("Y".equals(c.getIsshow())&&null == c.getIspk()){
					sb.append(GeneratorUtil.getSpace(4)+"<th data-options=\"field:'"+c.getPropertyname()+"',width:100\">"
				+(null==c.getComments()?c.getPropertyname():c.getComments())+"</th>\r\n");
				}		
		}
		return sb.toString();
	}

	public static String getEditTr(List<Column> columns) {
		/*<tr>
			<td>登录名:</td>
			<td><input class='easyui-textbox' style='width:150px' name='loginname' data-options='required:true'></input></td>
			<td>用户名:</td>
			<td><input class="easyui-textbox" style="width:150px" name="username" data-options="required:true"></input></td>
		</tr>*/
		StringBuffer sb = new StringBuffer();
		int count =0;
		for (Column c : columns) {
				if("Y".equals(c.getIspersistence())&&null==c.getIspk()){
					if(count%2==0){
						sb.append(GeneratorUtil.getSpace(4)+"<tr>\r\n");
					}
					sb.append(GeneratorUtil.getSpace(5)+"<td>"+(null==c.getComments()?c.getPropertyname():c.getComments())+":</td>\r\n");
					sb.append(GeneratorUtil.getSpace(5)+"<td><input class='"+getEasyuiTyoe(c)+"' style='width:150px' name='"+c.getPropertyname()+"' data-options='required:true'></input></td>\r\n");
					count++;
					if(count%2==0){
						sb.append(GeneratorUtil.getSpace(4)+"</tr>\r\n");
					}
					
				}
		}
		return sb.toString();
	}


	private static String getJdbctype(Column c) {
		String t = c.getJavatype();
		String rt ="VARCHAR";
		switch (t) {
		case "Integer":
			rt = "INTEGER";
			break;
		case "Long":
			rt = "BIGINT";
			break;
		case "Double":
			rt = "DOUBLE";
			break;
		case "String":
			rt = "VARCHAR";
			break;
		case "Date":
			rt = "DATE";
			break;
		case "DateTime":
			rt = "DATE";
			break;
		default:
			break;
		}
		return rt;
	}

	private static String getEasyuiTyoe(Column c) {
		String t = c.getJavatype();
		String rt ="easyui-textbox";
		switch (t) {
		case "Integer":
			rt = "easyui-numberbox";
			break;
		case "Long":
			rt = "easyui-numberbox";
			break;
		case "Double":
			rt = "easyui-numberbox";
			break;
		case "String":
			rt = "easyui-textbox";
			break;
		case "Date":
			rt = "easyui-datebox";
			break;
		case "DateTime":
			rt = "easyui-datetimebox";
			break;
		default:
			break;
		}
		return rt;
	}
}
